package com.stu.dao.impl;

import com.stu.dao.EmpDao;
import com.stu.entity.Emp;
import com.stu.util.JdbcTemplate;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author zy
 * @date 2025/3/4 10:02
 */
public class EmpDaoImpl implements EmpDao {
    JdbcTemplate template = new JdbcTemplate();

    public EmpDaoImpl() {
    }

    public EmpDaoImpl(JdbcTemplate template) {
        this.template = template;
    }

    public void setTemplate(JdbcTemplate template) {
        this.template = template;
    }

    @Override
    public int insert(Emp emp) {
        String sql = "insert into emp(empno,ename,job,mgr,hiredate,salary,comm,deptno) values(?,?,?,?,?,?,?,?)";
        return template.update(sql,emp.getEmpno(),emp.getEname(),emp.getJob(),emp.getMgr(),
                emp.getHiredate(),emp.getSalary(),emp.getComm(),emp.getDeptno());
    }

    @Override
    public int update(Emp emp) {
        String sql ="update emp set ename = ?,job = ?,mgr = ?,hiredate = ?, salary = ? , comm = ?, deptno = ? where empno = ?";
        return template.update(sql,emp.getEname(),emp.getJob(),emp.getMgr(),
                emp.getHiredate(),emp.getSalary(),emp.getComm(),emp.getDeptno(),emp.getEmpno());
    }

    @Override
    public int delete(int id) {
        String sql ="delete from emp where empno = ?";
        return template.update(sql,id);
    }

    @Override
    public Emp selectByPrimaryKey(int id) {
        return null;
    }

    @Override
    public List<Emp> selectAll() {
        return null;
    }

    public static void main(String[] args) {
        JdbcTemplate template = new JdbcTemplate();
        EmpDaoImpl dao = new EmpDaoImpl(template );

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse("1990-03-04 12:30:00", formatter);

        Emp emp = new Emp(8000,"李四","销售",7990,dateTime,4500,0,40);
        int row = 0;
//        row = dao.insert(emp);
//        row = dao.update(emp);
        row = dao.delete(8000);

        System.out.println(row);
    }
}
